#include <iostream>
#include <cstring>
#include "main.h"

using namespace std;

key_handle key_group[40];
key_handle *head,*tail;
int node_num=0;

char buf[512];

int str_search(const char* src, char* dst, int num) {		//在src查找dst第n次出现的首位置
    char* tmpsrc = (char*)src;
    //int pos = 0, len = strlen(src);
    while (num--) {
        tmpsrc = strstr(tmpsrc, dst);
        if (!tmpsrc)
            return -1;
        ++tmpsrc;
    }
    return (int)(tmpsrc - src) - 1;
}

//return byte number
int str_split(char* dst_buf,char* src_str,char* head,int head_num,char* tail,int tail_num){
    int inde_head=0,inde_tail=0;
    if(0>(inde_head=str_search(src_str,head,head_num)))
        return -1;
    ++inde_head;        //到达目标后部字串 头
    if(0>(inde_tail=str_search(src_str,tail,tail_num)))
        return -1;
    strncpy(dst_buf,&src_str[inde_head],(size_t)(inde_tail-(inde_head)));
    return inde_tail-(inde_head);
}

//void ef_set_env(char* key,char* val){
//    printf("%s:%s\n",key,val);      //模拟easyflash
//}
//char* ef_get_env(char* key){
//    return "ef_get_env";
//}

int insert_tail(key_handle* node){
    static int i=0;
    static int group_len=sizeof(key_group) / sizeof(key_handle);
    if(i==0){
        tail=node;          //初始时，头节点=表节点
        head=node;
        head->next=tail;
        head->previous=tail;
        tail->next=head;
        tail->previous=head;
    }else{
        tail->next=node;
        node->previous=tail;
        node->next=head;
        head->previous=node;
        tail=node;          //node成为尾节点
    }
    ++i;
    i %= group_len;
}
char* get_node_val(key_handle* node) {
    return node->val;
}


int get_node_index(key_handle* node,int num){
    int i=0;
    key_handle tmp;
    tmp=*head;
    for(i=0;i<node_num;i++){
        if(!strncmp(node->key,tmp.key,strlen(node->key))){
            if(num>1) {
                printf("忽略1个\n");
                --num;
            }else
                return i;
        }
        tmp=*tmp.next;
    }
    printf("not find node: %s\n",node->key);
    return -1;
}
/*
 * @E=1@T=1@DO1=1@D=10S@M=1@B=4800@C=9600@L=1,4
 *
 * 脚本执行到 L 指令时立即跳转到指定的 M 标记点，直到达到循环指定的次数后，继续往后面执行
 *
 * */

int unpack(char* buf,int buf_len){
    // static int node_num=0;
    char val_buf[25];
    int val_len=0;
    int i=0;
    static int group_len=sizeof(key_group) / sizeof(key_handle);
    for(;;) {
        ++i;
        memset(val_buf,0,sizeof(val_buf));

        key_group[node_num].key=(char*)malloc(sizeof(char)*4);
        memset(key_group[node_num].key,0,sizeof(char)*4);
        if(0>str_split(key_group[node_num].key,buf,"@",i,"=",i)){
            printf("key node_num: %d\n",node_num);
            return node_num;
        }

        if(0>(val_len=str_split(val_buf,buf,"=",i,"@",i+1))) {        //拿到末尾的val
            val_len=buf_len-(str_search(buf,"=",i)+1) ;
            strncpy(val_buf,&buf[str_search(buf,"=",i)+1],(size_t)val_len);
            //printf("tail val len: %d\n",buf_len-(str_search(buf,"=",i)+1));
        }
        key_group[node_num].val=(char*)malloc((sizeof(char)*val_len)+1);
        //memset(key_group[node_num].val,0,(sizeof(char)*val_len)+1);       //全部填充结束符
        strncpy(key_group[node_num].val,val_buf,(size_t)val_len);
        key_group[node_num].val[(sizeof(char)*val_len)]='\0';       //补上结束符

        //ef_set_env(key_group[node_num].key,val_buf);      //the same key saved to ef will be overwritten

        insert_tail(&key_group[node_num]);
        ++node_num;
        node_num %= group_len;
    }
}

void mark(key_handle* node){
    char val;

    //printf("val=%s\n",ef_get_env(node->key));

}
int execute(key_handle* handle,int len);

void loop(key_handle* node) {
    int L_index=0,mark_num=0,loop_num=0;
    int mark_tmp=0;
    key_handle tmp;
    sscanf(node->val,"%d,%d",&mark_num,&loop_num);
    printf("mark: %d loop: %d\n", mark_num,loop_num);
    if(0>(L_index=get_node_index(node,1))){
        printf("L not find M\n");
        return;
    }
    //printf("L_index: %d\n", L_index);
    tmp=*node;
    for (int i=0;i<L_index;i++) {
        tmp=*tmp.previous;
        //printf("find M: %s\n",tmp.val);
        if(!strncmp("M",tmp.key,strlen("M"))){      //往前 找到最近的 M

            sscanf(tmp.val,"%d",&mark_tmp);
            if(mark_tmp==mark_num){      //例：M=1     L=1,4
                printf("M -> L: %d\n",i);
                printf("M index %d\n",(L_index-i)-1);

                while(loop_num--) {
                    execute(&key_group[L_index-i],i);
                }
            }
        }

    }
}

cmd_handle group[]={			//字符串=>函数		对应回调表
        {"L",&loop},
        {"M",&mark},
};

int search_cmd_handle(key_handle* node){		//遍历查询表 回调
    static int i,cmd_len;
    cmd_len=sizeof(group)/sizeof(cmd_handle);
    for(i=0;i<cmd_len;i++){
        if(!strncmp(group[i].cmd,node->key,strlen(node->key))){
            if(group[i].func!=NULL){
                group[i].func(node);
                return 0;
            }else
                return -1;
        }
    }
    return -1;
}

int execute(key_handle* handle,int len){
    for(int i=0;i<len;i++){
        search_cmd_handle(&handle[i]);     //直接传node √  or 传node.key
    }
}

int main() {
    int len=0;
    key_handle key_tmp;
    for(;;) {
        memset(buf,0,sizeof(buf));
        cin >> buf;       //接收串口数据
        if(!strncmp("break",buf,strlen("break")))
            break;
        len=unpack(buf,strlen(buf));

        key_tmp=*head;
        for(int i=0;i<(len);i++){
            printf("%s->%s\n",key_tmp.key,key_tmp.val);
            key_tmp=*key_tmp.next;
        }
        execute(key_group,len);
//        for(int i=0;i<len;i++)
//            printf("key: %s\n",key_group[i].key);
    }
    return 0;
}